function getProperty(obj, chain, defaultVal) {
    const arr = chain.split(".");
    console.log(arr);
    for (let i = 0; i < arr.length; i++) {
        console.log(arr[i]);
        console.log(arr[i].split(''));
        const index = arr[i].split('').findIndex((val) => val === '[');
        console.log(index);
        if (index === -1) {
            if (obj[arr[i]]) {
                obj = obj[arr[i]];
            } else {
                return defaultVal;
            }
        } else {
            const pre = arr[i].split('').slice(0, index).join('');
            const last = arr[i].split('').slice(index + 1, arr[i].length - 1).join('');
            console.log(pre + "---" + last);
            if (Array.isArray(obj[pre]) && obj[pre][last]) {
                obj = obj[pre][last];
            } else {
                return defaultVal;
            }
        }
    }
    return obj;
}

const obj = {
    a: {
        b: [{
            c: 1
        }]
    }
}

let res = getProperty(obj, 'a.b[0].c.d', 9);
console.log(res);
